﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iwss.Document.Layouts.AjaxServices;
using Microsoft.SharePoint;
using System.Web;
using System.Collections;

namespace iwss.Document.Layouts.Services
{
    public class Other : IAjaxService
    {
        public SPList ShareLib { get { return SPContext.Current.Web.Lists.TryGetList("共享库"); } }
        public SPList DocLib { get { return SPContext.Current.Web.Lists.TryGetList("文档库"); } }
        public string CurrentUserDocumentZoneAddress { get { return string.Format("{0}/{1}", DocLib.RootFolder.Url, Utilitys.GetUserName(SPContext.Current.Web.CurrentUser.Name)); } }

        public AjaxResult Excute(HttpContext context)
        {
            string ActionType = context.Request["act"].ToLower();
            AjaxResult result = null;

            switch (ActionType)
            {
                case "history":
                    result = GetHistory(context);
                    break;
                case "linkman":
                    result = GetLinkman(context);
                    break;
            }

            return result;
        }

        private AjaxResult GetLinkman(HttpContext context)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取我共享的历史记录
        /// </summary>
        private AjaxResult GetHistory(HttpContext context)
        {
            ArrayList res = new ArrayList();
            SPQuery query = new SPQuery()
            {
                Query = string.Format(@"<Where> 
                                             <Eq>
                                                <FieldRef Name='Author' LookupId='True' />
                                                <Value Type='User'>{0}</Value>
                                             </Eq> 
                                       </Where>
                                       <OrderBy>
                                          <FieldRef Name='Modified' Ascending='False' />
                                       </OrderBy>", SPContext.Current.Web.CurrentUser.ID)
            };
            SPListItemCollection items = ShareLib.GetItems(query);

            res = GetShareFiles(items);

            return new AjaxResult(true, res);
        }

        private ArrayList GetShareFiles(SPListItemCollection items)
        {
            int itemCount = items.Count;
            ArrayList res = new ArrayList();
            for (int i = 0; i < itemCount; i++)
            {
                SPListItem item = items[i];
                res.Add(new
                {
                    Title = item["Title"],
                    ItemID = item.ID,
                    Url = item["iwss_Url"],
                    ModifyTime = item["Created"],
                    Sharer = Utilitys.GetUserName(item["iwss_Sharer"]),
                    FileItemID = item["iwss_LinkID"],
                    Icon = Utilitys.GetIcon(item["iwss_Url"].EToString()),
                    MyShare = true,
                    attr = new { target = "_blank" }
                });
            }
            return res;
        }

    }
}
